<?php
/*
Модуль вывода всех категорий магазина.
*/

defined('_JEXEC') or die('Restricted access');
if (!isset ($attribs['name'])) $attribs['name'] = 'catalog_list';
$cat_parent = ($attribs['name'] == 'catalog_list'?1:2);
if (!class_exists('ShopController')) {
    require_once (JPATH_BASE . DS . 'components/com_shop/controller.php');
}
$oConf = ShopController::conf();
$all_models_cats['1'] = $oConf->all_models_cats->dress;
$all_models_cats['2'] = $oConf->all_models_cats->sex;
echo '<ul class="catalog_list">';
$db = & JFactory::getDBO();
$query = "SELECT id,name FROM #__shop_categories WHERE parent = '{$cat_parent}' AND name!='' AND published=1 ORDER BY ordering ASC";
$db->setQuery($query);
$categories = $db->loadObjectList();
$cat_list = array();
if (!empty($categories)) {
foreach($categories as $key=>$category){
	$cat_list[] = $category->id;
}
$cat_list = implode(',',$cat_list);

$query = "SELECT cat_id, COUNT(*) AS item_count FROM #__shop_prod WHERE price>'0' AND published='1' AND main_img!='' AND cat_id IN (".$cat_list.") GROUP BY cat_id ORDER BY cat_id ASC";
$db->setQuery($query);
$counts = $db->loadObjectList('cat_id');
$query = "SELECT dublicated_cat, COUNT(*) AS item_count FROM #__shop_prod WHERE price>0 AND published='1' AND main_img!='' AND dublicated_cat IN (".$cat_list.") GROUP BY dublicated_cat ORDER BY dublicated_cat ASC";
$db->setQuery($query);
$dub_counts = $db->loadObjectList('dublicated_cat');

//echo '<pre>';
//var_dump($dub_counts);
//echo '</pre>';
$i=0;
foreach($categories as $key=>$category){
	
//	if($counts[$category->id]->item_count >0){
		$i++;
	//	$query = "SELECT id FROM #__shop_prod WHERE  price>0 AND cat_id=".$category->id." AND main_img != ''";
	//	$db->setQuery($query);
	//	$db->query();
	//	$item_count = $db->getNumRows();

                if(isset($dub_counts[$category->id])){
                    $dubs = (int)$dub_counts[$category->id]->item_count;
		}
		else
                    $dubs = 0;

		if(isset($counts[$category->id]->item_count)){
                    $count = $counts[$category->id]->item_count;
		}
		else
                    $count = 0;
                //if ( ($count+$dubs) == 0 ) continue;

		if(($i+1)%2 != 0)
                    echo '<li class="odd">';
		else
                    echo '<li>';

		//printu('Link: index.php?option=com_shop&view=category&id='.$category->id.' <br>');
		echo '<a href="index.php?option=com_shop&view=category&id='.$category->id.'">'.$category->name.'</a> ';
		if($count+$dubs>0){
			//echo $dub_counts[$category->id]->item_count;
		
			echo $count+$dubs;
		}
		else
			echo 0;
		echo '</li>';
//	}	
}

$query = "SELECT COUNT(p.id) AS count FROM #__shop_prod p
INNER JOIN #__shop_categories as c ON p.cat_id = c.id
WHERE  p.price>0 AND p.published='1' AND p.main_img != '' AND p.cat_id>0 AND c.parent = '{$cat_parent}'";
$db->setQuery($query);
$item_count = $db->loadResult();

if(($key+2)%2 != 0)
	echo '<li class="odd">';
else
	echo '<li>';
echo '<a href="index.php?option=com_shop&view=category&id=0">'.$all_models_cats[$cat_parent].'</a> '.$item_count;
echo '</li>';

echo '</ul>';
}
?>